package com.league.zjxn.main.mine

import android.os.Bundle
import android.view.View
import androidx.lifecycle.LifecycleOwner
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ColorUtils
import com.league.zjxn.R
import com.league.zjxn.entity.EnterpriseApprove
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.main.mine.adapter.ApproveFlowAdapter
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_approve_details.*

class ApproveDetailsActivity : OwnerViewModeActivity<UserViewModel>() {

    companion object {
        private const val KEY_APPROVE_DATA = "KEY_APPROVE_DATA"
        fun open(approve: EnterpriseApprove) {
            val bundle = Bundle().apply {
                putSerializable(KEY_APPROVE_DATA, approve)
            }
            ActivityUtils.startActivity(bundle, ApproveDetailsActivity::class.java)
        }
    }

    private val mApproveFlowAdapter = ApproveFlowAdapter()

    private val mEnterpriseApprove: EnterpriseApprove? by lazy {
        intent?.extras?.getSerializable(KEY_APPROVE_DATA) as? EnterpriseApprove
    }

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_approve_details

    override fun stateBar(): StatusBar {
        return StatusBar(view = flApproveDetailsBar)
    }

    override fun initView() {

        rvApproveDetailsFlowBody.adapter = mApproveFlowAdapter

        mEnterpriseApprove?.let { fillData(it) }
    }

    override fun initData() {
        mEnterpriseApprove?.let {
            viewModel.userApproveFlow(it.id)
        }
    }

    override fun initViewMode() {
        viewModel.approveFlowLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data?.data
                ?: return@observe
            mApproveFlowAdapter.setNewInstance(data)
        })
    }

    private fun fillData(approve: EnterpriseApprove) {
        val color = when (approve.status) {
            5 -> R.color.FF_17B572
            6 -> R.color.FF_FF3527
            else -> R.color.FF_FF8127
        }
        tvApproveDetailsIsShow.visibility = if (approve.is_show == 1) View.VISIBLE else View.GONE
        tvApproveDetailsStatusText.setTextColor(ColorUtils.getColor(color))
        tvApproveDetailsStatusText.text = approve.status_text
        tvApproveDetailsTitle.text = approve.name
        tvApproveDetailsContact.text = approve.person
        tvApproveDetailsMobile.text = approve.contract
        tvApproveDetailsProvince.text = approve.province
        tvApproveDetailsIndustry.text = approve.industry
        tvApproveDetailsAppeal.text = approve.content
    }
}